# -*- coding: utf-8 -*-
import urllib,urllib2,cookielib
import os,sys,time
import re

# Request URL:http://f.10086.cn/im5/index/queryFriends.action?fromUrl=main&from=1&_=1326963677867
# Request Method:GET
# Status Code:200 OK
# Request Headersview source
# Accept:application/json, text/javascript, */*
# Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3
# Accept-Encoding:gzip,deflate,sdch
# Accept-Language:en-US,zh-CN;q=0.8,zh;q=0.6
# Connection:keep-alive
# Cookie:WT_FPC=id=211.140.39.195-500956624.30115731:lv=1326961338894:ss=1326961235084; JSESSIONID=219C6783DA98DCB8558A2E2CB5EFF17F.4401; user=13957861878; pass=33g02g04; cell_cookie=b5c26219730d4b83a3e01161d25686d1|09431270ad042e08ddc33e51de704474; loginstatusCookie=400
# Host:f.10086.cn
# Referer:http://f.10086.cn/im5/index/html5.action?t=35782006793003847
# User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.18 (KHTML, like Gecko) Chrome/18.0.1010.1 Safari/535.18
# Query String Parametersview URL encoded
# fromUrl:main
# from:1
# _:1326963677867
# Response Headersview source
# Connection:keep-alive
# Content-Language:zh-CN
# Content-Type:text/json; charset=utf-8
# Date:Thu, 19 Jan 2012 09:01:21 GMT
# Server:nginx/0.7.61
# Transfer-Encoding:chunked

'''
{"total":5,"friendTotal":3,"contacts":[{"idContact":288160052,"localName":"ice","relationStatus":1,"contactType":0,"isBlocked":0,"permission":"identity\u003d1;","idFetion":713921405,"mobileNo":"13566031076","email":"","carrier":"CMCC","carrierStatus":0,"basicServiceStatus":1,"services":"99","smsOnlineStatus":"0.0:0:0","presenceBasic":"0","presenceDesc":"","deviceType":"","uri":"sip:713921405@fetion.com.cn;p\u003d1561","nickname":"ice","impresa":"","impresaLength":0,"newMsgNums":0,"portraitCrc":"-164095478"},{"idContact":291376594,"localName":"灰兔","relationStatus":1,"contactType":0,"isBlocked":0,"permission":"identity\u003d1;","idFetion":595173769,"mobileNo":"13586827430","email":"","carrier":"CMCC","carrierStatus":0,"basicServiceStatus":1,"services":"99","smsOnlineStatus":"0.0:0:0","presenceBasic":"0","presenceDesc":"","deviceType":"","uri":"sip:595173769@fetion.com.cn;p\u003d1769","nickname":"灰兔","impresa":"熊猫人，鸟人，植物人","impresaLength":18,"newMsgNums":1,"portraitCrc":"0"},{"idContact":345124391,"localName":"游客","relationStatus":1,"contactType":0,"isBlocked":0,"permission":"identity\u003d1;","idFetion":632548988,"mobileNo":"","email":"","carrier":"CMCC","carrierStatus":0,"basicServiceStatus":1,"services":"99","smsOnlineStatus":"365.0:0:0","presenceBasic":"0","presenceDesc":"","deviceType":"","uri":"sip:632548988@fetion.com.cn;p\u003d6245","nickname":"游客","impresa":"","impresaLength":0,"newMsgNums":0,"portraitCrc":"0"},{"idContact":300139237,"localName":"857571023","relationStatus":0,"contactType":0,"isBlocked":0,"permission":"identity\u003d1;","idFetion":857571023,"mobileNo":"","email":"","carrier":"CMCC","carrierStatus":0,"basicServiceStatus":1,"services":"99","smsOnlineStatus":"365.0:0:0","presenceBasic":"0","presenceDesc":"","deviceType":"","uri":"sip:857571023@fetion.com.cn;p\u003d3222","nickname":"","impresa":"","impresaLength":0,"newMsgNums":0,"portraitCrc":"0"},{"idContact":291360922,"localName":"游客","relationStatus":2,"contactType":0,"isBlocked":0,"permission":"identity\u003d1;","idFetion":562702200,"mobileNo":"","email":"","carrier":"CMCC","carrierStatus":-1,"basicServiceStatus":1,"services":"","smsOnlineStatus":"","presenceBasic":"0","presenceDesc":"","deviceType":"","uri":"sip:562702200@fetion.com.cn;p\u003d1766","nickname":"游客","impresa":"","impresaLength":0,"newMsgNums":0,"portraitCrc":"0"}]}
'''


def fetion(msg):
    url_login = 'http://f.10086.cn/im/login/inputpasssubmit1.action'
    url_logout = 'http://f.10086.cn/im/index/logoutsubmit.action?t='
    url_mymsg = 'http://f.10086.cn/im/user/sendMsgToMyselfs.action'
#    url_tomsg = 'http://f.10086.cn/im5/chat/sendNewShortMsg.action'
    user = '13957861878'
    password = '33g02g04'
    loginstatus = '4'
    arg_t = ''
    cj = cookielib.LWPCookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    urllib2.install_opener(opener)
    args = {'pass':password, 'm':user,'loginstatus':loginstatus}
    print 'Logining...'
    req = urllib2.Request(url_login, urllib.urlencode(args))
    jump = opener.open(req)
    page = jump.read();
    url = re.compile(r'<card id="start".*?ontimer="(.*?);').findall(page)[0] 
    arg_t = re.compile(r't=(\d*)').findall(page)[0]
    if url == '/im/login/login.action':
        print "error"
        return
    sendmsg = urllib2.Request(url_mymsg, urllib.urlencode({'msg':msg}))
    finish = urllib2.urlopen(sendmsg)

    if finish.geturl == 'http://f.10086.cn/im/user/sendMsgToMyself.action' :
        print 'Send Failed!'
    else:
        print 'Send Successfully'
    logout = urllib2.Request(url_logout + arg_t)
    response = urllib2.urlopen(logout) 
    print 'Logout Successfully!'
    print response.read().decode('utf-8').encode('gbk')
fetion('aaa')